#!/usr/bin/ruby

require 'rbconfig'

####################################################################################################
# Each time a file is added or removed, the script must be re-run and a new project file generated.#
####################################################################################################

base_dir = ENV['PROC_MOD_BASE_DIR']
if base_dir == nil then puts 'Failure: PROC_MOD_BASE_DIR is not set'; exit end

sys_name = ''
rm_string = ''
case RbConfig::CONFIG['host_os']
when /linux/i
  sys_name = 'linux'
  rm_string = 'rm '
when /darwin/i
  sys_name = 'mac'
  rm_string = 'rm '
end

project_file_name='%PROJECTNAME%.pro'

##########################
# Cleanup Existing Files #
##########################
if File.exists?(project_file_name) then system(rm_string + project_file_name) end
if File.exists?(project_file_name + '.tmp') then system(rm_string + project_file_name + '.tmp') end
if File.exists?('Makefile') then system(rm_string + 'Makefile') end
if File.exists?('Makefile.Debug') then system(rm_string + 'Makefile.Debug') end
if File.exists?('Makefile.Release') then system(rm_string + 'Makefile.Release') end

#############################
# Generate initial PRO file #
#############################
system('qmake -project . ../../src -o %s.tmp'%project_file_name)

#########################
# Create final PRO file #
#########################
initial_file = File.open(project_file_name + '.tmp','r')
final_file = File.open(project_file_name,'w')

initial_file.each_line do |line|

  if /^TEMPLATE.+/ =~ line then
    final_file.puts('TEMPLATE = lib')
    final_file.puts('QT += opengl')
    final_file.puts('CONFIG += debug_and_release plugin build_all')
    final_file.puts('INCLUDEPATH += ../../Source ../../../Common/include/')
    final_file.puts('DESTDIR = ../../Plugins')
    final_file.puts('QMAKE_CXXFLAGS_RELEASE += -O3 -fvisibility=hidden')
    final_file.puts('QMAKE_LIBDIR += '+base_dir+'/Common/lib')

    if(sys_name == 'mac')
      final_file.puts('INCLUDEPATH += /usr/local/include /usr/include /opt/local/include')
      final_file.puts('LIBS += -lGLEW -L/usr/lib/ -L/usr/local/lib -L/opt/local/lib')
      final_file.puts('QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.6')
    elsif sys_name == 'linux'
      final_file.puts('QMAKE_CXX = g++-4.3')
    end

  elsif /^TARGET.+/ =~ line
    final_file.puts('CONFIG(debug, debug|release) {')
    final_file.puts('TARGET = $$join(TARGET,,,_d)')
    final_file.puts('LIBS += -lProcModel_d')
    final_file.puts('}')
    final_file.puts('CONFIG(release, debug|release) { LIBS += -lProcModel }')
  else
    final_file.puts(line)
  end

end

initial_file.close
final_file.close

system(rm_string + '%s.tmp'%project_file_name)

##########################################
# Generate platform specific build files #
##########################################
case sys_name
when 'mac'
  system('qmake -spec macx-g++ ' + project_file_name)
when 'linux'
  system('qmake -makefile ' + project_file_name)
end
